/*
 *所有关于wareHouse类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*wareHouseService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using Base.Service.IRepositories;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using BusFin.Read.Entity.DomainModels;
using SqlSugar;

namespace Base.Service.Services
{
    public partial class WareHouseService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IWareHouseRepository _repository;//访问数据库
        private readonly ISqlSugarClient _sqlSugarClient;

        [ActivatorUtilitiesConstructor]
        public WareHouseService(
            IWareHouseRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            ISqlSugarClient sqlSugarClient
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _sqlSugarClient = sqlSugarClient;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        WebResponseContent webResponse = new WebResponseContent();

        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //此处saveModel是从前台提交的原生数据，可对数据进修改过滤
            AddOnExecute = (SaveModel saveModel) =>
            {
                //如果返回false,后面代码不会再执行
                return webResponse.OK();
            };
            // 在保存数据库前的操作，所有数据都验证通过了，这一步执行完就执行数据库保存
            AddOnExecuting = (wareHouse order, object list) =>
            {
               
                //明细表对象
                List<wareHouse> orderLists = new List<wareHouse>();

                ////自定义逻辑
                //if (orderLists == null || orderLists.Count == 0)
                //{//如果没有界面上没有填写明细，则中断执行
                //    return webResponse.Error("必须填写明细数据");
                //}
                orderLists=  _sqlSugarClient.Queryable<wareHouse>().ToList();
                if (orderLists.Exists(x => x.wareHouseName ==""))
                {
                    return webResponse.Error("仓库名称不能为空");
                }
                if (orderLists.Where(c => c.wareHouseName == order.wareHouseName).Count() > 0)
                {
                    return webResponse.Error("仓库名称不能重复");
                }

                //设置webResponse.Code = "-1"会中止后面代码执行，与返回 webResponse.Error()一样，区别在于前端提示的是成功或失败
                //webResponse.Code = "-1";
                // webResponse.Message = "测试强制返回";
                //return webResponse.OK("ok");

                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }

        public override WebResponseContent Update(SaveModel saveModel)
        {
            //编辑方法保存数据库前处理
            UpdateOnExecuting = (wareHouse order, object addList, object updateList, List<object> delKeys) =>
            {
                List<wareHouse> orderLists = new List<wareHouse>();

                orderLists = _sqlSugarClient.Queryable<wareHouse>().ToList();
                if (orderLists.Exists(x => x.wareHouseName == ""))
                {
                    return webResponse.Error("仓库名称不能为空");
                }
                if (orderLists.Where(c => c.wareHouseName == order.wareHouseName&&c.wareHouseId!=order.wareHouseId).Count() > 0)
                {
                    return webResponse.Error("仓库名称不能重复");
                }


                //设置webResponse.Code = "-1"会中止后面代码执行，与返回 webResponse.Error()一样，区别在于前端提示的是成功或失败
                //webResponse.Code = "-1";
                // webResponse.Message = "测试强制返回";
                //return webResponse.OK("ok");

                return webResponse.OK();
            };

            return base.Update(saveModel);
        }

    }
}
